package com.zuorenke.db.extend;

import org.apache.ibatis.session.RowBounds;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;

/**
 * @author Z.R.K
 * @description
 * @create 2019-04-12 16:03:07
 **/
public class Page<T> extends RowBounds implements Serializable {
	
	private static final long serialVersionUID = -26145989260946286L;
	private static final int OFFSET_IGNORE = -1;
	private static final int DFT_CUR_PAGE = 1;
	private static final int DFT_PAGE_SIZE = 10;
	private static final int DFT_PAGE_COUNT = 10;
	
	private int curPage = DFT_CUR_PAGE;
	private int pageSize = DFT_PAGE_SIZE;
	
	private int totalSize;
	private int totalPage;
	
	private int offset = OFFSET_IGNORE; // 当直接设置 offset 时，使用该 offset 值,而不从 curPage 和 pageSize 来计算
	
	private List<T> dataList = Collections.emptyList();
	
	private int pageCount = DFT_PAGE_COUNT; // 分页显示的页数
	
	public Page() {
		this(DFT_CUR_PAGE, DFT_PAGE_SIZE);
	}
	
	public Page(int curPage, int pageSize) {
		this.curPage = curPage;
		this.pageSize = pageSize;
	}
	
	public int getCurPage() {
		return curPage;
	}
	
	public void setCurPage(int curPage) {
		this.curPage = curPage < DFT_CUR_PAGE ? DFT_CUR_PAGE : curPage;
	}
	
	public int getPageSize() {
		return pageSize;
	}
	
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize <= 0 ? DFT_PAGE_SIZE : pageSize;
	}
	
	public int getTotalSize() {
		return totalSize;
	}
	
	public int getTotalPage() {
		return totalPage;
	}
	
	public void setTotalSize(int totalSize) {
		this.totalSize = totalSize;
		
		if (0 != totalSize) {
			totalPage = totalSize % pageSize == 0 ? totalSize / pageSize : totalSize / pageSize + 1;
			this.curPage = this.curPage > totalPage ? totalPage : curPage;
		}
	}
	
	/*
	 * 获取 offset
	 */
	@Override
	public int getOffset() {
		if (offset >= 0) {
			return offset;
		} else {
			return curPage > 0 ? (curPage - 1) * pageSize : 0;
		}
	}
	
	/**
	 * 设置 offset
	 *
	 * @param val
	 * @return 当前对象
	 */
	public Page offset(int val) {
		this.offset = val;
		return this;
	}
	
	/*
	 * 获取 limit
	 */
	@Override
	public int getLimit() {
		return pageSize;
	}
	
	/**
	 * 设置 limit
	 *
	 * @param val
	 * @return
	 */
	public Page limit(int val) {
		setPageSize(val);
		return this;
	}
	
	public void setDataList(List<T> dataList) {
		this.dataList = dataList;
	}
	
	public List<T> getDataList() {
		return dataList;
	}
	
	/**
	 * @return the pageCount
	 */
	public int getPageCount() {
		return pageCount;
	}
	
	/**
	 * @param pageCount the pageCount to set
	 */
	public void setPageCount(int pageCount) {
		this.pageCount = pageCount <= 0 ? DFT_PAGE_COUNT : pageCount;
	}
	
	public int getStartPage() {
		int endPage = getEndPage();
		int startPage = endPage - pageCount + 1;
		if(startPage < 1){
			startPage = 1;
		}
		return startPage;
	}
	
	public int getEndPage() {
		int endPage = curPage + pageCount / 2 - 1;
		if (endPage < pageCount) {
			endPage = pageCount;
		}
		if (endPage > totalPage) {
			endPage = totalPage;
		}
		return endPage;
	}
	
}